//
//  RateConfig.m
//  Rong360
//
//  Created by tianliwei on 15/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "Config.h"
#import "ParseUtil.h"
static Config* config = nil;

@implementation Config

+ (Config *)shareConfig{
    
    @synchronized(self){
        
        if ( config == nil) {
            config = [[Config alloc] init];
        }
    }
    
    return config;
}

- (void)getConfigFromServerUrl:(NSString *)url{
    NSURL *_url = [NSURL URLWithString:url];
    NSMutableURLRequest *_request = [NSMutableURLRequest requestWithURL:_url];
    [NSURLConnection sendAsynchronousRequest:_request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) {
                               //block define statment
                               NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                               NSLog(@"response status code is %d",[httpResponse statusCode]);
                               if (error) {
                                   NSLog(@"%@",error);
                                   [self getConfigFromCache];//如果没有网络，从缓存读取
                               }else{
                                   [[ParseUtil shareParser] getConfig:data];
                               }
                           }];
}

- (void)getCitiesFromServerUrl:(NSString *)url{
    NSURL *_url = [NSURL URLWithString:url];
    NSMutableURLRequest *_request = [NSMutableURLRequest requestWithURL:_url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0f];
    [NSURLConnection sendAsynchronousRequest:_request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) {
                               //block define statment
                               NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                               NSLog(@"response status code is %d",[httpResponse statusCode]);
                               if (error) {
                                   NSLog(@"%@",error);

                               }else{
                                   [[ParseUtil shareParser] getCities:data];
                               }
                           }];
}


- (void)getDefaultCity{
    
}


- (void)getDefaultConfig{
    config.yingyeshui = @"5.6";
    config.geren_quan = @"1";
    config.geren_cha = @"20";
    config.chenjian = @"7";
    config.jiaoyufujia = @"3";
    config.dijiakuan_quan = @"10";
    config.dijiakuan_cha = @"70";
    config.qishui_1dang = @"1";
    config.qishui_2dang = @"1.5";
    config.qishui_3dang = @"3";
    config.jizhun_house = @"6.55";
    config.jizhun_gjj = @"4.5";
    config.zhongjie = @"2.7";
    config.yinghua = @"0";
    
    //保存到本地缓存
    [[NSUserDefaults standardUserDefaults]setObject:config.yingyeshui forKey:@"yinyeshui"];
    [[NSUserDefaults standardUserDefaults]setObject:config.geren_quan forKey:@"geren_quan"];
    [[NSUserDefaults standardUserDefaults]setObject:config.geren_cha forKey:@"geren_cha"];
    [[NSUserDefaults standardUserDefaults]setObject:config.chenjian forKey:@"chenjian"];
    [[NSUserDefaults standardUserDefaults]setObject:config.jiaoyufujia forKey:@"jiaoyufujia"];
    [[NSUserDefaults standardUserDefaults]setObject:config.dijiakuan_quan forKey:@"dijiakuan_quan"];
    [[NSUserDefaults standardUserDefaults]setObject:config.dijiakuan_cha forKey:@"dijiakuan_cha"];
    [[NSUserDefaults standardUserDefaults]setObject:config.qishui_1dang forKey:@"qishui_1dang"];
    [[NSUserDefaults standardUserDefaults]setObject:config.qishui_2dang forKey:@"qishui_2dang"];
    [[NSUserDefaults standardUserDefaults]setObject:config.qishui_3dang forKey:@"qishui_3dang"];
    [[NSUserDefaults standardUserDefaults]setObject:config.zhongjie forKey:@"zhongjie"];
    [[NSUserDefaults standardUserDefaults]setObject:config.jizhun_house forKey:@"house_jizhun"];
    [[NSUserDefaults standardUserDefaults]setObject:config.jizhun_gjj forKey:@"gongjijin_jizhun"];
    [[NSUserDefaults standardUserDefaults]setObject:config.yinghua forKey:@"yinghua"];
}

- (void)getConfigFromCache{
    config.yingyeshui = [[NSUserDefaults standardUserDefaults]objectForKey:@"yinyeshui"];
    config.geren_quan = [[NSUserDefaults standardUserDefaults]objectForKey:@"geren_quan"];
    config.geren_cha = [[NSUserDefaults standardUserDefaults]objectForKey:@"geren_cha"];
    config.chenjian = [[NSUserDefaults standardUserDefaults]objectForKey:@"chenjian"];
    config.jiaoyufujia = [[NSUserDefaults standardUserDefaults]objectForKey:@"jiaoyufujia"];
    config.dijiakuan_quan = [[NSUserDefaults standardUserDefaults]objectForKey:@"dijiakuan_quan"];
    config.dijiakuan_cha = [[NSUserDefaults standardUserDefaults]objectForKey:@"dijiakuan_cha"];
    config.qishui_1dang = [[NSUserDefaults standardUserDefaults]objectForKey:@"qishui_1dang"];
    config.qishui_2dang = [[NSUserDefaults standardUserDefaults]objectForKey:@"qishui_2dang"];
    config.qishui_3dang = [[NSUserDefaults standardUserDefaults]objectForKey:@"qishui_3dang"];
    config.jizhun_house = [[NSUserDefaults standardUserDefaults]objectForKey:@"house_jizhun"];
    config.jizhun_gjj = [[NSUserDefaults standardUserDefaults]objectForKey:@"gongjijin_jizhun"];
    config.zhongjie = [[NSUserDefaults standardUserDefaults]objectForKey:@"zhongjie"];
    config.yinghua = [[NSUserDefaults standardUserDefaults]objectForKey:@"yinghua"];

}
@end
